<?php
defined('IN_MAIN') or exit('Invalid Access!');

function calc_matching_dist2($in_year, $in_month, $in_adjust_percent){
/*
This function takes all the matching bonus for the month and will automatically adjust the payout in such that all the bonus
payout (including matching bonus, exclude stockist fee) is at 55%.

On successful commission calculation, it will return boolean true, else it will return the error messages
*/

	global $vars;
	$db=$vars["db"];
	$projected_payout_percent = $vars['projected_payout_percent'];
	$projected_payout_percent = $in_adjust_percent;
	$maintenance_percent = $vars['maintenance_percent'];
	$max_maintain = $vars['max_maintain'];
	
	$start_time=mktime(0, 0, 0, $in_month, 1, $in_year);
	$calc_date['start']=date('Y-m-d H:i:s', mktime(0, 0, 0, $in_month, 1, $in_year));
	$calc_date['end']=date('Y-m-d H:i:s', mktime(23, 59, 59, $in_month+1, 0, $in_year));
	$maintain_time=mktime(0, 0, 0, $in_month+1, 1, $in_year);
	$maintain_year = date("Y", $maintain_time);
	$maintain_month = date("n", $maintain_time);
	
	$qi = 0;
	$quid = array();
	$t = @mysql_num_rows($r=mysql_query("select distinct(uid) as uid from $db->member_comm where type='m' and year='$in_year' and month='$in_month' and status='confirmed'"));
	for($i=0;$i<$t;$i++){
		$comm = mysql_fetch_assoc($r);
		$uid = $comm['uid'];
		$total_sale = @mysql_result(mysql_query("select sum(total_bv) from $db->purchase where uid='$uid' and no_bv!='y' and status='confirmed' and cdate>='$calc_date[start]' and cdate<='$calc_date[end]'"), 0) or 0;
		$total_maintain = @mysql_result(mysql_query("select sum(amount) from $db->member_maintain where uid='$uid' and year='$in_year' and month='$in_month'"), 0) or 0;
		$ps = $total_sale + $total_maintain;
		if($ps >= $max_maintain){
			$quid[$qi++] = $uid;
		}
	}
	if(count($quid)){
		$matching_total = @mysql_result(mysql_query("select sum(amount) from $db->member_comm where type='m' and uid in (".implode(',', $quid).") and year='$in_year' and month='$in_month' and status='confirmed'"), 0) or 0;
		$other_total = @mysql_result(mysql_query("select sum(amount) from $db->member_comm where type in ('r','p') and year='$in_year' and month='$in_month' and status='confirmed'"), 0) or 0;
		$sales_total=@mysql_result(mysql_query("select sum(total_bv) from $db->purchase where status='confirmed' and no_bv!='y' and cdate>='$calc_date[start]' and cdate<='$calc_date[end]'"), 0) or 0;
		$pool_total = 0;
		$pool_total+=round($sales_total*$vars['pool'][1]['percent']/100, 2);
		$pool_total+=round($sales_total*$vars['pool'][2]['percent']/100, 2);
		$pool_total+=round($sales_total*$vars['pool'][3]['percent']/100, 2);
		$comm_total = $matching_total + $other_total + $pool_total;
		
		$payout_percent = $comm_total / $sales_total * 100;
		$matching_percent = $matching_total / $sales_total * 100;
		$matching_adjusted = false;
		if($payout_percent > $projected_payout_percent){
			$diff_percent = $payout_percent - $projected_payout_percent;
			if($diff_percent > $matching_percent){
				$adjust_matching_percent = 0;
				$percent2 = 100;
			}else{
				$adjust_matching_percent = $matching_percent - $diff_percent;
				$adjust_matching_percent = round($adjust_matching_percent / $matching_percent * 100, 2);
				$percent2 = $adjust_matching_percent;
			}
			$matching_adjusted = true;
		}else{
			$percent2 = 100;
		}
		echo "percent: $percent2\n";
	}
	
	return array('status'=>$status, 'errmsg'=>$errmsg);
}
?>